package com.itheima.first_demo;

import com.itheima.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test02 {
    /*
    步骤:  建表
            -->导包log4j-1.2.17.jar   mybatis-3.5.3.jar   mysql-connector-java-5.1.37-bin.jar
            -->domain类
            -->映射配置文件 StudentMapper src文件夹下
            -->核心配置文件 MyBatisConfig 同上
            -->测试类
     */
    private SqlSession sqlSession;

    @Before
    public void before() throws IOException {
        //1.加载MyBatisConfig.xml核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        //2.创建sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();

        //3.使用factoryBuilder创建SqlSession
        SqlSessionFactory factory = factoryBuilder.build(is);

        //4.使用factory创建SqlSession
        sqlSession = factory.openSession();
    }

    @After
    public void after() {
        //7.释放资源
        sqlSession.close();
    }

    //1.查询全部
    @Test
    public void selectAll() {

        //5.调用sqlSession方法查询
        List<Student> list = sqlSession.selectList("StudentMapper.selectAll");

        //处理结果
        for (Student stu : list) {
            System.out.println(stu);
        }
    }

    //2.根据id查询
    @Test
    public void selectOne() {
        //5.调用sqlSession方法查询
        Student stu = sqlSession.selectOne("StudentMapper.selectOne", 2);

        //处理结果
        System.out.println(stu);
    }

    //3.模糊查询  #{ }方式
    @Test
    public void selectByName() {
        //5.调用sqlSession方法查询
        List<Student> list = sqlSession.selectList("StudentMapper.selectByName", "王莉");

        //处理结果
        for (Student stu : list) {
            System.out.println(stu);
        }
    }

    //4.添加数据
    @Test
    public void insert() {
        Student student = new Student(5, "张俊", 23, "男");
        //5.调用sqlSession方法查询
        int i = sqlSession.insert("StudentMapper.insert", student);
        sqlSession.commit();
        //处理结果
        System.out.println(i);
    }

    //5,修改数据
    @Test
    public void update() {
        Student student = new Student(12, "黄美琪", 25, "男");
        //5.调用sqlSession方法查询
        int update = sqlSession.update("StudentMapper.update", student);
        sqlSession.commit();
        //处理结果
        System.out.println(update);
    }

    //6.删除数据
    @Test
    public void delete() {
        //5.调用sqlSession方法查询
        int delete = sqlSession.delete("StudentMapper.delete", 11);
        sqlSession.commit();
        //处理结果
        System.out.println(delete);
    }

    //7.查询的返回值深入(手动映射)
    @Test
    public void selectDeep() {
        //5.调用sqlSession方法查询
        int delete = sqlSession.delete("StudentMapper.delete", 4);
        //处理结果
        System.out.println(delete);
    }


}
